package com.itcam.phaselist.list_;

import java.util.Vector;

/**
 * 1.Vector类的定义说明<p>
 * public class Vector<E> extends AbstractList<E> implements List<E>,RandomAccess,Cloneable,Serailizable<p>
 * 2.Vector底层也是一个对象数组，protected Object[] elementData<p>
 * 3.Vector是线程同步的，即线程安全，Vector类的操作方法带有synchronized<p>
 * 4.在开发中，需要线程同步安全时，考虑使用Vector<p>
 */

public class Vector_ {
    public static void main(String[] args) {
        // 无参构造器
//        Vector vector = new Vector();
        // 有参构造器
        Vector vector = new Vector(8);
        for (int i = 0; i < 10; i++) {
            vector.add(i);
        }

        vector.add(100);
        System.out.println("vector = " + vector);

        // 1.new Vector底层
        /**
         * 无参的
         * public Vector(){
         *     this(10);
         * }
         */
        /**
         * 有参的
         * public Vector(int initialCapacity){
         *     this(initialCapacity,0);
         * }
         */

        // 2.vector.add()底层
        /**
         * 2.1 下面这个方法就添加数据到vector集合
         * public synchronized boolean add(E e) {
         *      modCount++;
         *      ensureCapacityHelper(elementCount + 1);
         *      elementData[elementCount++] = e;
         *      return true;
         * }
         */
        /**
         * 2.2 确定是否需要扩容条件： minCapacity - elementData.length>0
         * private void ensureCapacityHelper(int minCapacity) {
         *      // overflow-conscious code
         *      if (minCapacity - elementData.length > 0)
         *          grow(minCapacity);
         * }
         */
        /**
         * 2.3 如果需要的数组大小不够用，就扩容, 扩容的算法:newCapacity = oldCapacity+((capacityIncrement > 0)?capacityIncrement:oldCapacity);
         * private void grow(int minCapacity) {
         *      // overflow-conscious code
         *      int oldCapacity = elementData.length;
         *      int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
         *      if (newCapacity - minCapacity < 0)
         *          newCapacity = minCapacity;
         *      if (newCapacity - MAX_ARRAY_SIZE > 0)
         *          newCapacity = hugeCapacity(minCapacity);
         *      elementData = Arrays.copyOf(elementData, newCapacity);
         * }
         */
    }
}
